<?php
class Zendvn_System_Plugin extends Zend_Controller_Plugin_Abstract
{
	   
	public function preDispatch(Zend_Controller_Request_Abstract $request){
		//echo '<br>' . __METHOD__;
      	$acl = new Zendvn_Acl();

      	/*
      	 * Kiem tra xem co phai dang truy phan Admin hay khong
      	 */
      	$controllerName = $request->getControllerName();
      	$moduleName = $request->getModuleName();
      	$flagAdmin = false;
      	if($controllerName == 'admin'){
      		$flagAdmin = true;
      	}else{
      		$tmp = explode('-', $controllerName);      		
      		if(current($tmp) == 'admin'){
      			$flagAdmin = true;
      		}
      	}
      	
      	if($moduleName == 'user'){
      		$flagAdmin = true;
      		$flagAccess = 'PUBLIC';
      	}
      	
		$action = $request->getActionName();
		
      	$flagAccess = false;
      	if($flagAdmin == true){
      	 	if($acl->isFullAccess()!= true && $moduleName != 'user'){
				if($acl->isMember() != 'Member'){
	      			$flagAccess = 'LOGIN';
	      		}else{
	      			if($acl->isAdminAccess() != true){
                        $flagAccess = 'NO-ACCESS';
                        if(in_array($controllerName,array('admin-event','admin-news-items'))){
                            $flagAccess = false;
                        }

	      			}
	      		}
      		}
	      	if(!$acl->isMember()){
	      		if($moduleName == 'user'){
	      			$flagAccess = 'PUBLIC';
	      		}
	      	}

      	}
      	
		if($action == 'uploadify' || $action == 'upload-images' || $action == 'upload-file' || $action == 'download'){
			$flagAdmin == false;
			$flagAccess = false;
		}
      	
		switch ($flagAccess){
      		case "LOGIN": 	
      						$request->setModuleName('admin');
      						$request->setControllerName('index');
      						$request->setActionName('login');
      						break;
      		case "NO-ACCESS": 	
      						$request->setModuleName('admin');
      						$request->setControllerName('index');
      						$request->setActionName('no-access');
      						break;
      		case "PUBLIC":
      						$request->setModuleName('default');
      						$request->setControllerName('index');
      						$request->setActionName('index');
      						break;
      	}
    }
	
}